package com.tjshop.android;

import java.util.List;

import com.tjshop.android.adapter.RegionAdapter;
import com.tjshop.android.model.MyListItem;
import com.tjshop.android.util.AddressUtil;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class SelectRegionActivity extends BaseActivity implements
		OnItemClickListener {

	TextView info;
	ListView regions;
	private int curMode = 1;//1-province,2-city,3-area
	private String pCode,cCode,aCode;
	private List<MyListItem> curList;
	@Override
	protected void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_select_region);
		
		setTheme(R.style.Theme_Sherlock);
		updateCommonReturnBar("选择地区");
		info = (TextView) findViewById(R.id.info);
		regions = (ListView) findViewById(R.id.regions);
		
		initListView();
		
		regions.setOnItemClickListener(this);
		
		
	}
	
	private void initListView() {
		switch (curMode) {
		case 1:
			curList = AddressUtil.getAllProvince(getApplicationContext());
			break;
		case 2:
			curList = AddressUtil.getCity(getApplicationContext(),pCode);
			break;
		case 3:
			curList = AddressUtil.getArea(getApplicationContext(),cCode);
			break;
		default:
			break;
		}
		regions.setAdapter(new RegionAdapter(getApplicationContext(), curList));
	}


	@Override
	public boolean handleMessage(Message arg0) {
		return false;
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		if(curMode == 1){
			pCode = curList.get(arg2).getPcode();
			curMode = 2;
			info.setVisibility(View.VISIBLE);
			info.setText(curList.get(arg2).getName());
			initListView();
		}else if(curMode == 2){
			cCode = curList.get(arg2).getPcode();
			curMode = 3;
			info.setVisibility(View.VISIBLE);
			info.setText(info.getText().toString()+curList.get(arg2).getName());
			initListView();
		}else if(curMode == 3){
			info.setVisibility(View.VISIBLE);
			info.setText(info.getText().toString()+curList.get(arg2).getName());
			Intent it = new Intent();  
            it.putExtra("region", info.getText().toString());  
            setResult(Activity.RESULT_OK, it);  
            finish();  
		}
	}

}
